<!DOCTYPE html>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>highlight.do</title>
<meta name="generator" content="KF5::SyntaxHighlighting (Stata)"/>
</head><body style="color:#1f1c1b"><pre>
<span style="color:#898887;">/* Test file for kate's stata syntax highlighting</span>
<span style="color:#898887;">*/</span>
*! <span style="font-weight:bold;">version</span> 1.2.0 2mar2003 E. Leuven
<span style="font-weight:bold;">program</span> <span style="font-weight:bold;">define</span> spellsplit
        <span style="font-weight:bold;">version</span> 7
        <span style="font-weight:bold;">syntax</span> [anything], spell(<span style="font-weight:bold;">varlist</span> <span style="color:#644a9b;">min</span>=2 <span style="color:#644a9b;">max</span>=2) [ <span style="font-weight:bold;">by</span>(<span style="font-weight:bold;">varlist</span>)]
        <span style="font-weight:bold;">tokenize</span> <span style="color:#006e28;">`spell'</span>
        <span style="font-weight:bold;">local</span> date0 <span style="color:#006e28;">`1'</span>
        <span style="font-weight:bold;">local</span> date1 <span style="color:#006e28;">`2'</span>
        <span style="font-weight:bold;">local</span> f0 : <span style="font-weight:bold;">format</span> <span style="color:#006e28;">`date0'</span>
        <span style="font-weight:bold;">local</span> f1 : <span style="font-weight:bold;">format</span> <span style="color:#006e28;">`date1'</span>

        <span style="color:#898887;">/* set default statistic */</span>
        <span style="font-weight:bold;">local</span> current <span style="color:#bf0303;">&quot;mean&quot;</span>

        gettoken left anything : anything, <span style="color:#644a9b;">match</span>(prns)
        <span style="font-weight:bold;">while</span> <span style="color:#bf0303;">&quot;`left'&quot;</span>!=<span style="color:#bf0303;">&quot;&quot;</span> {
                <span style="font-weight:bold;">if</span> <span style="color:#bf0303;">&quot;`prns'&quot;</span>!=<span style="color:#bf0303;">&quot;&quot;</span> {
                        <span style="font-weight:bold;">if</span> !<span style="color:#644a9b;">inlist</span>(<span style="color:#bf0303;">&quot;`left'&quot;</span>,<span style="color:#bf0303;">&quot;mean&quot;</span>,<span style="color:#bf0303;">&quot;sum&quot;</span>) {
                                <span style="font-weight:bold;">di</span> <span style="font-weight:bold;">as</span> <span style="font-weight:bold;">error</span> <span style="color:#bf0303;">&quot;Statistic `left' not supported&quot;</span>
                                <span style="font-weight:bold;">exit</span> 198
                        }
                        <span style="font-weight:bold;">local</span> current <span style="color:#bf0303;">&quot;`left'&quot;</span>
                }
                <span style="font-weight:bold;">else</span> {
                        <span style="font-weight:bold;">local</span> <span style="color:#006e28;">`current'</span>vars  <span style="color:#006e28;">``current'</span>vars' <span style="color:#006e28;">`left'</span>
                }
                gettoken left anything : anything, <span style="color:#644a9b;">match</span>(prns)
        }
        <span style="font-weight:bold;">if</span> (<span style="color:#bf0303;">&quot;`meanvars'&quot;</span>!=<span style="color:#bf0303;">&quot;&quot;</span>) {
                <span style="font-weight:bold;">confirm</span> <span style="font-weight:bold;">var</span> <span style="color:#006e28;">`meanvars'</span>
                <span style="font-weight:bold;">unab</span> meanvars : <span style="color:#006e28;">`meanvars'</span>
        }
        <span style="font-weight:bold;">if</span> (<span style="color:#bf0303;">&quot;`sumvars'&quot;</span>!=<span style="color:#bf0303;">&quot;&quot;</span>) {
                <span style="font-weight:bold;">confirm</span> <span style="font-weight:bold;">var</span> <span style="color:#006e28;">`sumvars'</span>
                <span style="font-weight:bold;">unab</span> sumvars : <span style="color:#006e28;">`sumvars'</span>
        }

        <span style="font-weight:bold;">quietly</span> {
                g _count = 1
                <span style="font-weight:bold;">local</span> xvars <span style="color:#006e28;">`meanvars'</span> <span style="color:#006e28;">`sumvars'</span> _count

                <span style="color:#898887;">/* create dummy by-var if no by option is specified */</span>
                <span style="font-weight:bold;">if</span> <span style="color:#bf0303;">&quot;`by'&quot;</span>==<span style="color:#bf0303;">&quot;&quot;</span> {
                        <span style="font-weight:bold;">tempvar</span> <span style="font-weight:bold;">by</span>
                        g <span style="font-weight:bold;">byte</span> <span style="color:#006e28;">`by'</span> = 1
                }
                <span style="font-weight:bold;">tempvar</span> <span style="color:#006e28;">`xvars'</span> <span style="color:#006e28;">`by'</span>

                <span style="color:#898887;">/* create negative for subtraction when spell ends */</span>
                cap <span style="font-weight:bold;">foreach</span> v <span style="font-weight:bold;">of</span> <span style="font-weight:bold;">varlist</span> <span style="color:#006e28;">`xvars'</span> {
                        g <span style="font-weight:bold;">double</span> <span style="color:#006e28;">``v'</span>' = -<span style="color:#006e28;">`v'</span>
                        <span style="font-weight:bold;">local</span> txvars <span style="color:#006e28;">`txvars'</span> <span style="color:#006e28;">``v'</span>'
                }
                cap <span style="font-weight:bold;">foreach</span> v <span style="font-weight:bold;">of</span> <span style="font-weight:bold;">varlist</span> <span style="color:#006e28;">`by'</span> {
                        g <span style="font-weight:bold;">double</span> <span style="color:#006e28;">``v'</span>' = <span style="color:#006e28;">`v'</span>
                        <span style="font-weight:bold;">local</span> txvars <span style="color:#006e28;">`txvars'</span> <span style="color:#006e28;">``v'</span>'
                }

                <span style="font-weight:bold;">stack</span> <span style="color:#006e28;">`date0'</span> <span style="color:#006e28;">`xvars'</span> <span style="color:#006e28;">`by'</span> <span style="color:#006e28;">`date1'</span> <span style="color:#006e28;">`txvars'</span>, into(<span style="color:#006e28;">`date0'</span> <span style="color:#006e28;">`xvars'</span> <span style="color:#006e28;">`by'</span>) <span style="font-weight:bold;">clear</span>

                <span style="color:#898887;">/* calculate totals per date */</span>
                cap <span style="font-weight:bold;">foreach</span> v <span style="font-weight:bold;">of</span> <span style="font-weight:bold;">varlist</span> <span style="color:#006e28;">`xvars'</span> {
                        <span style="font-weight:bold;">egen</span> <span style="font-weight:bold;">double</span> <span style="color:#006e28;">``v'</span>' = <span style="font-weight:bold;">sum</span>(<span style="color:#006e28;">`v'</span>), <span style="font-weight:bold;">by</span>(<span style="color:#006e28;">`by'</span> <span style="color:#006e28;">`date0'</span>)
                }

                <span style="color:#898887;">/* unique dates only */</span>
                <span style="font-weight:bold;">by</span> <span style="color:#006e28;">`by'</span> <span style="color:#006e28;">`date0'</span>, <span style="font-weight:bold;">sort</span>: <span style="font-weight:bold;">keep</span> <span style="font-weight:bold;">if</span> <span style="color:#0057ae;">_n</span>==1

                <span style="color:#898887;">/* calculate totals (+ when spell starts - when ends) */</span>
                <span style="font-weight:bold;">sort</span> <span style="color:#006e28;">`by'</span>
                cap <span style="font-weight:bold;">foreach</span> v <span style="font-weight:bold;">of</span> <span style="font-weight:bold;">varlist</span> <span style="color:#006e28;">`xvars'</span> {
                        <span style="font-weight:bold;">by</span> <span style="color:#006e28;">`by'</span>: <span style="font-weight:bold;">replace</span> <span style="color:#006e28;">`v'</span> = <span style="font-weight:bold;">sum</span>(<span style="color:#006e28;">``v'</span>')
                }
                <span style="font-weight:bold;">by</span> <span style="color:#006e28;">`by'</span>: g <span style="color:#006e28;">`date1'</span> = <span style="color:#006e28;">`date0'</span>[<span style="color:#0057ae;">_n</span> + 1]

                <span style="font-weight:bold;">drop</span> <span style="font-weight:bold;">if</span> <span style="color:#006e28;">`date0'</span>&gt;<span style="color:#006e28;">`date1'</span>
                <span style="font-weight:bold;">drop</span> _stack
                <span style="font-weight:bold;">drop</span> <span style="font-weight:bold;">if</span> _count==0
                <span style="font-weight:bold;">order</span> <span style="color:#006e28;">`by'</span> <span style="color:#006e28;">`date0'</span> <span style="color:#006e28;">`date1'</span> <span style="color:#006e28;">`xvars'</span>
                <span style="font-weight:bold;">format</span> <span style="color:#006e28;">`date0'</span> <span style="color:#006e28;">`f0'</span>
                <span style="font-weight:bold;">format</span> <span style="color:#006e28;">`date1'</span> <span style="color:#006e28;">`f1'</span>

                cap <span style="font-weight:bold;">for</span> <span style="font-weight:bold;">var</span> <span style="color:#006e28;">`meanvars'</span>: <span style="font-weight:bold;">replace</span> X = X/_count

                <span style="font-weight:bold;">compress</span>
        }

<span style="font-weight:bold;">end</span>
</pre></body></html>
